/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mygame;

import com.jme3.asset.AssetManager;
import com.jme3.audio.AudioNode;
import com.jme3.audio.AudioRenderer;
import com.jme3.audio.Listener;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;

/**
 *
 * @author Kiview
 */
public class AudioManager {

	private static final float DEFAULT_BGM_VOLUME = 0.5f;

	private static final float DEFAULT_SFX_VOLUME = 0.5f;

	private static final String BGM_PATH = "Sounds/BGM/";

	private static final String AUDIO_FORMAT = ".ogg";

	/** Background music. */
	private AudioNode bgm;

	/** For playing soundEffects. */
	private AudioNode sfx;

	/** The audio renderer. */
	private AudioRenderer audioRenderer;

	/** Our asset manager. */
	private AssetManager assetManager;

	private Listener listener;

	private Camera cam;


	public AudioManager() {
		Game game = Game.getInstance();
		audioRenderer = game.getAudioRenderer();
		assetManager = game.getAssetManager();
		listener = game.getListener();
		cam = game.getCamera();
	};

	public void initAudio() {
		setBGM("intro");
	}

	public void setBGM(String fileName) {
		alignListenerToCam();

		bgm = new AudioNode(assetManager, BGM_PATH + fileName + AUDIO_FORMAT, false);
		bgm.setLooping(true);

		bgm.setPositional(false);
		bgm.setVolume(DEFAULT_BGM_VOLUME);

		audioRenderer.playSource(bgm);
	}

	/**
	 * For now this is just an example, we need a structure which is able to
	 * play a number of effects simultaniously.
	 */
	public void playSoundEffect(String fileName, Vector3f position) {
		sfx = new AudioNode(assetManager, fileName, false);
		sfx.setLooping(false);
		sfx.setVolume(DEFAULT_BGM_VOLUME);
		sfx.setLocalTranslation(position);
		audioRenderer.playSource(sfx);
	}

	public void alignListenerToCam() {
		listener.setLocation(cam.getLocation());
    listener.setRotation(cam.getRotation());
	}

}
